/**
 * 
 */
package com.net.shop.car.manager.ui.viewholder;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import android.graphics.Bitmap;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.net.shop.car.manager.BaseActivity;
import com.net.shop.car.manager.R;
import com.net.shop.car.manager.api.model.Goods;
import com.net.shop.car.manager.api.model.Seller;
import com.net.shop.car.manager.api.model.SellerInfo;
import com.net.shop.car.manager.api.volley.VolleyCenter;
import com.net.shop.car.manager.utils.StringUtils;

/**
 * @author Deve
 *
 */
public class LujiuViewHolder implements ViewHolder,OnGoodsAddedShopCarListener {

	private BaseActivity activity;

	private ImageView logo;
	private TextView shopName;
	private TextView good;
	private TextView notGood;
	private TextView des;
	private TextView distance;
	private TextView address;
	
	private ListView tels;
	
	private TextView timeDes;
	private TextView time;
	private ListView serviceingListView;
	private ListView notServicingListView;
	private TextView switchBtn;
	private JieSuanViewHolder jieSuanViewHolder;
	private boolean isDayService;

	private String timeZone;
	
	/**
	 * @param view
	 * @param activity
	 */
	public LujiuViewHolder(View view,BaseActivity activity) {
		this.activity = activity;

		logo = (ImageView) view.findViewById(R.id.seller_shop_logo);
		shopName = (TextView) view.findViewById(R.id.seller_shop_name);
		good = (TextView) view.findViewById(R.id.seller_shop_good);

		notGood = (TextView) view.findViewById(R.id.seller_shop_notgood);

		des = (TextView) view.findViewById(R.id.seller_store_des);
		distance = (TextView) view
				.findViewById(R.id.seller_shop_distance);
		address = (TextView) view.findViewById(R.id.seller_shop_address);
		
		tels = (ListView) view.findViewById(R.id.way_save_tel);
		time = (TextView) view.findViewById(R.id.way_save_time);
		timeDes = (TextView) view.findViewById(R.id.way_save_des);
		switchBtn = (TextView) view.findViewById(R.id.way_save_btn);
		serviceingListView = (ListView) view.findViewById(R.id.way_save_servicing_goods);
		notServicingListView = (ListView) view.findViewById(R.id.way_save_noinservicing_goods);
		
		jieSuanViewHolder = new JieSuanViewHolder(view.findViewById(R.id.way_save_pay_panel),activity);
	}

	@Override
	public void setData(Seller seller, final SellerInfo sellerInfo) {
		Bitmap bitmap = sellerInfo.getShopLogoBitmap();
		if (bitmap == null) {
			bitmap = seller.getShopLogoBitmap();
			sellerInfo.setShopLogoBitmap(bitmap);
			if (bitmap == null) {
				VolleyCenter.getVolley().addImageRequest(
						sellerInfo.getShopLogo(),
						new Response.Listener<Bitmap>() {

							@Override
							public void onResponse(Bitmap response) {
								sellerInfo.setShopLogoBitmap(response);
								logo.setImageBitmap(response);
							}
						}, new Response.ErrorListener() {

							@Override
							public void onErrorResponse(VolleyError error) {
//								logo.setImageBitmap(ImageUtils.decodeResource(activity.getResources(), 
//										R.drawable.wupeitu, 0, 0));// 设置默认图片
							}
						});
			} else {
				logo.setImageBitmap(bitmap);
			}
		} else {
			logo.setImageBitmap(bitmap);
		}
		shopName.setText(sellerInfo.getShopName());
		good.setText(sellerInfo.getGoodNum() + "");
		notGood.setText(sellerInfo.getErrNum() + "");
		des.setText(sellerInfo.getIntroduce());
		distance.setText(StringUtils.parseDistance(seller.getDistance()));
		address.setText(sellerInfo.getShopAddress());
		
		setTel(sellerInfo.getTels());
		
		switchBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (serviceingListView.getVisibility() == View.VISIBLE) {
					serviceingListView.setVisibility(View.GONE);
					notServicingListView.setVisibility(View.VISIBLE);
					if(isDayService) {
						setBtnText(false,sellerInfo.getServiceTwoDes());
					} else {
						setBtnText(false,sellerInfo.getServiceOneDes());
					}
				} else {
					serviceingListView.setVisibility(View.VISIBLE);
					notServicingListView.setVisibility(View.GONE);
					setBtnText(true,timeZone);
				}
			}
		});
		String day = sellerInfo.getServiceOneDes();
		isDayService = isInTimeZone(day);
		if(isDayService) {
			timeZone = sellerInfo.getServiceOneDes();
			timeDes.setText("当前服务是白天");
			serviceingListView.setAdapter(new ServiceGoodsAdapter(sellerInfo.getServiceOne(),sellerInfo.isOnlinePay(),activity,this));
			notServicingListView.setAdapter(new ServiceGoodsAdapter(sellerInfo.getServiceTwo(),false,activity,this));
		} else {
			timeZone = sellerInfo.getServiceTwoDes();
			timeDes.setText("当前服务是晚上");
			notServicingListView.setAdapter(new ServiceGoodsAdapter(sellerInfo.getServiceOne(),false,activity,this));
			serviceingListView.setAdapter(new ServiceGoodsAdapter(sellerInfo.getServiceTwo(),sellerInfo.isOnlinePay(),activity,this));
		}
		setBtnText(true,timeZone);
		 
	}
	/**
	 * 
	 * @param isService
	 */
	private void setBtnText(boolean isService,String time) {
		if(isDayService ) {
			if(isService) {
				switchBtn.setText("白天");
			} else {
				switchBtn.setText("晚上");
			}
			
		} else {
			if(isService) {
				switchBtn.setText("晚上");
			} else {
				switchBtn.setText("白天");
			}
		}
		this.time.setText(time);
	}
	
	private boolean isInTimeZone(String timeZone) {
		String[] zone = timeZone.split("-");
		DateFormat df = new SimpleDateFormat("hh:mm");
		if(zone.length == 2) {
			try {
				long time1 = df.parse(zone[0]).getTime(); 
				long time2 = df.parse(zone[1]).getTime();
				long time = System.currentTimeMillis();
				if(time >= time1 && time <= time2 ) {
					return true;
				}
				
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return false;
	}
	
	private void setTel(final List<String> tel) {
		tels.setAdapter(new TelsAdpater(tel, activity));
	}

	@Override
	public void onGoodsAdded(Goods goods) {
		jieSuanViewHolder.addGoods(goods);
	}

	@Override
	public void onGoodsRemoved(Goods goods) {
		jieSuanViewHolder.addGoods(goods);
	}
}
